package com.soft.zosagent.zoscls;

import com.ibm.jzos.MvsConsole;
import com.ibm.jzos.WtoConstants;
import com.ibm.jzos.ZUtil;

/**
 * @description:
 * @author: cinbo，chenyinbo
 * @create: 2020-12-03 14:46
 */
public class MvsCommandUtil {

    public static void WaitConsoleCmd(){
        try{
            MyMvsCommandCallback mvsCallback = new MyMvsCommandCallback();
            if (!MvsConsole.isListening()) {
                MvsConsole.startMvsCommandListener();
            }
            MvsConsole.registerMvsCommandCallback(mvsCallback);

            String jobname = ZUtil.getCurrentJobname();
            while (true) {
                MvsConsole.wto("Java console interaction:\n   f " + jobname + ",APPL=QUIT\n   p " + jobname,
                        WtoConstants.ROUTCDE_PROGRAMMER_INFORMATION,
                        WtoConstants.DESC_IMPORTANT_INFORMATION_MESSAGES);
                synchronized (mvsCallback) {
                    // wait until a modify command is received.  If a stop command is received,
                    // the program will exit from the callback and will not return here.
                    mvsCallback.wait();
                }
                if ("QUIT".equalsIgnoreCase(mvsCallback.modifyCommand)) {
                    break;
                }

            }
            MvsConsole.wto("EXIT command received.",
                    WtoConstants.ROUTCDE_PROGRAMMER_INFORMATION,
                    WtoConstants.DESC_IMPORTANT_INFORMATION_MESSAGES);
            System.out.println("Finished processing.");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
